package uk.co.richardwkirk.parkrun.android.events;

import java.util.ArrayList;

import uk.co.richardwkirk.parkrun.data.events.Event;
import uk.co.richardwkirk.parkrun.geo.Region;

public class RegionalEventGroup {

    private ArrayList<Event> events = new ArrayList<Event>();
    private String regionName;

    public void addEvent(Event event) {
        events.add(event);
    }

    public void setRegionName(String name, int count) {
    	StringBuilder nameBuilder = new StringBuilder();
    	nameBuilder.append(name).append(" (").append(count).append(")");
        regionName = nameBuilder.toString();
    }

    public ArrayList<Event> getEntries() {
        return events;
    }

    public String getRegionName() {
        return regionName;
    }

	public static ArrayList<RegionalEventGroup> createFromCountry(Region country) {
		ArrayList<RegionalEventGroup> eventGroups = new ArrayList<RegionalEventGroup>();
		if (country.countRegions() == 0) {
			eventGroups.add(createFromRegion(country));
		}
		else {
			for (Region region : country.getSortedSubRegions()) {
				eventGroups.add(createFromRegion(region));
			}
		}
		return eventGroups;
	}
	
	public static RegionalEventGroup createFromRegion(Region region) {
		RegionalEventGroup eventGroup = new RegionalEventGroup();
		eventGroup.setRegionName(region.getName(), region.countEvents());
		for (Event event : region.getSortedEvents()) {
			eventGroup.addEvent(event);
		}
		return eventGroup;
	}
    
    
}
